//
//  FDLeftController.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/5.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDLeftController.h"
#import "FDApplyController.h"
#import "FDCaseMangerController.h"
#import "FDAttentionController.h"
#import "FDSetingController.h"
#import "FDAboutController.h"
#import "FDShareController.h"

#import "FDLoginView.h"
#import "FDNonLoginView.h"
#import "FDLoginController.h"
#import "FDRegisterController.h"

#import "WZHUserDefaults.h"
#import "FDPersonController.h"

@interface FDLeftController ()<UITableViewDataSource,UITableViewDelegate>

//左控制器的上边的用户资料视图
@property (weak,nonatomic) UIView * headerView;

//左控制器的tableView
@property (weak,nonatomic) UITableView * tableView;

//保存第一组的标题
@property (strong,nonatomic) NSArray * section1TitleArray;
//保存第二组的标题
@property (strong,nonatomic) NSArray * section2TitltArray;
//保存第一组标题对应的控制器名
@property (strong,nonatomic) NSArray * section1VCArray;
//保存第二组标题对应的控制器名
@property (strong,nonatomic) NSArray * section2VCArray;

@property (weak,nonatomic) UIImageView * welcomeImageView;



@end




@implementation FDLeftController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self setupUI];
    
}

/**
 *  isLogin的set方法
 */
- (void)setIsLogin:(BOOL)isLogin{
    
    _isLogin = isLogin;
    //清除界面
    if (self.headerView) {
        self.headerView = nil;
    }
  
    //如果当前状态为已登陆
    if (isLogin) {
        [self addLoginUserView];
        
        self.welcomeImageView.hidden = YES;
        self.tableView.hidden = NO;
        [self addLoginLeftView];
        
    }else{
        //如果当前状态为未登陆
        [self addNonLoginUserView];
        
        self.welcomeImageView.hidden = NO;
        self.tableView.hidden = YES;
        
        
        [self addNonLoginLeftView];
    }

    
    
}


/**
 *  设置界面
 */
- (void)setupUI{
    
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    //添加上面的用户资料视图
    
    //从偏好设置读取当前的登陆状态,并设=设置相应的界面
    self.isLogin = [WZHUserDefaults WZHBoolForKey:@"isLogin"];
    
    
}

- (void)addLoginLeftView{
    
    //添加tableView--功能视图
    [self addFunctionView];

}

- (void)addNonLoginLeftView{
    
    //添加一张imageView
    
    UIImageView * welcomeImageView =  [[UIImageView alloc]init];
    
    welcomeImageView.image = [UIImage imageNamed:@"welcome_image"];
    
    self.welcomeImageView = welcomeImageView;
    
    [self.view addSubview:welcomeImageView];
    
    [welcomeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.offset(250);
        make.height.offset(216);
        make.leading.offset(35);
        make.centerY.equalTo(self.view.mas_centerY).offset(-20);
        
    }];
    
}


/**
 *  添加未登陆时的用户资料视图
 */
- (void)addNonLoginUserView{
    
    
    FDNonLoginView *headerView = [[FDNonLoginView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 130)];
    
    self.headerView = headerView;
    
    [self.view addSubview:headerView];
    
    headerView.nonLoginViewBlock = ^(UIButton *btn){
        
        if ([btn.titleLabel.text isEqualToString:@"登陆"]) {
            //如果点击的是登陆按钮,跳转到登陆界面
            FDLoginController *logVC = [[FDLoginController alloc]init];
            
            [self jumpToOtherViewController:logVC];
            
        }else if ([btn.titleLabel.text isEqualToString:@"注册"]){
            
            //如果点击的是注册按钮,跳转到注册界面
            FDRegisterController *registerVC = [[FDRegisterController alloc]init];
            [self jumpToOtherViewController:registerVC];
            
        }      
        
    };
    
}



/**
 *  添加已登陆时上面的用户资料视图
 */
- (void)addLoginUserView{
    
    FDLoginView *headerView = [[FDLoginView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 130)];
    
    
    self.headerView = headerView;
    
    [self.view addSubview:headerView];
    
    headerView.loginViewBlock = ^{
        
        NSLog(@"跳转");
        
        FDPersonController *personVC = [[FDPersonController alloc]init];
        
        [self jumpToOtherViewController:personVC];
        
    };
    
    
    
}

/**
 *  添加tableView
 */
- (void)addFunctionView{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height, SCREEN_WIDTH, SCREEN_HEIGHT-self.headerView.frame.size.height) style:UITableViewStyleGrouped];
    
    self.tableView = tableView;
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    
    //    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.leading.offset(0);
    //        make.trailing.offset(0);
    //        make.top.equalTo(self.headerView.mas_bottom).offset(0);
    //        make.bottom.equalTo(0);
    //
    //    }];
    
    
}


#pragma mark - tableView datasource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        //让第一个间距消失
        return 0.1;
    }
    
    return 20;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (indexPath.section == 0) {
        cell.textLabel.text = self.section1TitleArray[indexPath.row];
    }else if (indexPath.section == 1){
        
        cell.textLabel.text = self.section2TitltArray[indexPath.row];
    }
    
    return cell;
}

#pragma mark  - tableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //点击cell跳转到相应的控制器
    NSArray *vcArray = [NSArray array];
    
    NSArray *titleArray = [NSArray array];
    
    //获取对应的控制器名和标题
    if (indexPath.section == 0) {
        BOOL islogin = [WZHUserDefaults WZHBoolForKey:@"isLogin"];
        
        vcArray =@[@"FDApplyController",islogin ? @"FDCaseMangerController" : @"FDLoginController",@"FDAttentionController"];
        titleArray = self.section1TitleArray;
    }else if (indexPath.section == 1){
            vcArray = self.section2VCArray;
        titleArray = self.section2TitltArray;
    }
    
    NSString *vcStr = vcArray[indexPath.row];
    
    //实例化目标跳转的控制器
    FDLog(@"%d",[WZHUserDefaults WZHBoolForKey:@"isLogin"]);
    FDLog(@"%@",vcStr);
    UIViewController *destinationVC = [[NSClassFromString(vcStr) alloc]init];
    //设置目标控制器的标题
    destinationVC.title = titleArray[indexPath.row];
    
    //跳转到目标控制器
    [self jumpToOtherViewController:destinationVC];
    
    
}


#pragma mark - 懒加载

- (NSArray *)section1TitleArray{
    
    if (!_section1TitleArray) {
        _section1TitleArray = @[@"名医通申请",@"病例管理",@"关注医生"];
    }
    
    return _section1TitleArray;
}

- (NSArray *)section2TitltArray{
    
    if (!_section2TitltArray) {
        _section2TitltArray = @[@"设置与帮助",@"关于产品",@"分享给好友"];
    }
    return _section2TitltArray;
}

- (NSArray *)section1VCArray{
    
    if (!_section1VCArray) {
        
       
        _section1VCArray = @[@"FDApplyController",@"FDLoginController",@"FDAttentionController"];
    }
    
    return _section1VCArray;
}

- (NSArray *)section2VCArray{
    
    if (!_section2VCArray) {
        _section2VCArray = @[@"FDSetingController",@"FDAboutController",@"FDShareController"];
    }
    
    return _section2VCArray;
}


-(void)dealloc{
    
  //  NSLog(@"左控制器挂了");
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
//    NSLog(@"左控制器出现了");
    
}




@end
